import listentry
import util
import template
import uicls
import uiutil
_defaultWidth = _defaultHeight = 250

def Make_Container(d, ids, ignoreMocks = True):
    ("\n    Makes a window that has no visible parts but may contain other windows, thus\n    helping organize them.\n    \n    Attributes:\n      - id\n      - name\n      - align\n      - width, height (default %(defaultWidth)sx%(defaultHeight)s.)\n      - state (default pickChildren)\n      - left, top (default 0, 0)\n      - xmargin, ymargin (default: the value of 'margin')\n      - margin (default: no margin)\n      - clipper: (default: no) If true, I'll clip the parts, in all \n                 descendants, which fall out of the rectangle defined by me.\n      - children\n      - position\n    " % {'defaultWidth': _defaultWidth,
     'defaultHeight': _defaultHeight})
    align = template.AlignFromStr(d.get('align', 'none'))
    autoPos = template.PositionFromStr(d.get('position', 'default'))
    newAlign = uiutil.GetAlign(align, 0, autoPos)
    ret = uicls.Container(align=newAlign, width=d.get('width', _defaultWidth), height=d.get('height', _defaultHeight), state=template.StateFromStr(d.get('state', 'pickChildren')), left=d.get('left', 0), top=d.get('top', 0), xmargin=d.get('xmargin', d.get('margin', None)), ymargin=d.get('ymargin', d.get('margin', None)), clipChildren=d.get('clipper', False))
    template.MakeChildren(ret, d.get('children', []), ids, ignoreMocks)
    return ret



def Make_Scroll(d, ids, ignoreMocks = True):
    ret = uicls.Scroll(template.MakeFromYaml(('\n        type: Container\n        align: %(align)s\n        ' % {'align': d.get('align', 'none')})))

    def GetColumnText(d):
        ret = getattr(mls, d['caption'])
        if d.get('breakSpaces', False):
            ret = ret.replace(' ', '<br>')
        return ret


    columns = map(GetColumnText, d.get('columns', []))
    entries = [ listentry.Get(e['entryType'], util.KeyVal(**e)) for e in d.get('entries', []) if not((ignoreMocks and e.get('mock', False))) ]
    if columns:
        old = ret.state
        eve.triapp.uilib.desktop.children.append(ret)
    ret.liveWithoutParent = True
    ret.Startup()
    ret.sr.id = d.get('id', None)
    if (columns or entries):
        ret.Load(contentList=entries, headers=columns)
    if columns:
        eve.triapp.uilib.desktop.children.remove(ret)
        ret.state = old
    return ret


exports = util.AutoExports('template', locals())

